import { createRouter, createWebHistory,createWebHashHistory } from "vue-router";
import home from "../views/home/index.vue";
import { setSession,getSession } from "../utils/index.js";
import store from '@/store/index.js'

const routes = [
  {
    path: "/",
    name: "home",
    component: home,
    meta:{
     
      index:1
    }
  },

  {
    path: "/goodCard",
    name: "goodCard",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/goodsCard/goodCard.vue"),
    meta:{
      isLogin:true,
      index:2
    }
  },
  {
    path: "/order",
    name: "order",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/order/order.vue"),
    meta:{
      isLogin:true,
      index:3
    }
  },
  {
    path: "/my",
    name: "my",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/my/my.vue"),
    meta:{
      isLogin:true,
      index:4
    }
  },
  {
    path: "/myAddress",
    name: "myAddress",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/my/myAddress.vue"),
    meta:{
      isLogin:true
    }
  },
  {
    path: "/builtAddress/:judge",
    name: "builtAddress",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/my/builtAddress.vue"),
    meta:{
      isLogin:true
    }
  },
  {
    path: "/updateAddress",
    name: "updateAddress",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/my/updateAddress.vue"),
    meta:{
      isLogin:true
    }
  },
  {
    path: "/classify/:index",
    name: "classify",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/classify/classify.vue"),
  },
  {
    path: "/goodsDetail/:id",
    name: "goodsDetail",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/home/goodsDetail.vue"),
  
  },
  {
    path: "/search",
    name: "search",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/search/search.vue"),
    // redirect: "/search/default",
    // meta:{
    //   isLogin:true
    // },
    // children: [
    //   {
    //     path: "default",
    //     component: () => import("../views/search/components/default.vue"),
    //     meta: {
    //       index: 1,
    //     },
    //   },
    //   {
    //     path: "searchResult",
    //     component: () => import("../views/search/components/searchResult.vue"),
    //     meta: {
    //       index: 2,
    //     },
    //   },
    // ],
  },
  {
    path: "/addOrder/:num",
    name: "addOrder",
    component: () =>
    import(/* webpackChunkName: "about" */ "../views/order/addOrder.vue"),
    meta:{
      isLogin:true
    }
  },
  
  {
    path: "/allLogin",
    name: "allLogin",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/login/allLogin.vue"),
    
    // children: [
    //   {
    //     path: "login",
    //     component: () => import("../views/login/components/login.vue"),
    //     meta: {
    //       index: 1,
    //     },
    //   },
    //   {
    //     path: "regiest",
    //     component: () => import("../views/login/components/regiest.vue"),
    //     meta: {
    //       index: 2,
    //     },
    //   },
    // ],
  },
];

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes,
  //每次跳转页面都来到滚动条滚动至最顶部
  // scrollBehavior () {
  //   return { left: 0, top: 0 }
  // }
});
const judge=(toIndex,fromIndex)=>{
  if (toIndex&&fromIndex) {
  
    if (toIndex>fromIndex) {
      store.commit("changeRouterType", "push");
    }else{
      store.commit("changeRouterType", "back");
    }
  } else if(toIndex&&!fromIndex) {
    store.commit("changeRouterType", "back");
  }else if(!toIndex&&fromIndex) {
    store.commit("changeRouterType", "push");
  }
}
router.beforeEach((to,from,next)=>{


  if(to.meta.isLogin){ 
    judge(to.meta.index,from.meta.index)
    
   let result=getSession("result")
   console.log();
    if (!result) {
     router.push('/allLogin?redirct='+to.fullPath)
    } else {

     next()
    }
  }else{
    judge(to.meta.index,from.meta.index)
   next()
  }
  
 });
 

export default router;
